package com.sunday.scoremanager.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.adapter.CommenAdapter;
import com.sunday.common.adapter.ViewHolder;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.Bank;
import com.sunday.scoremanager.entity.BankAccount;
import com.sunday.scoremanager.http.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/11/7.
 */

public class AddBankAccountActivity extends BaseActivity {

    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.edit_name)
    ClearEditText editName;
    @Bind(R.id.edit_id_num)
    ClearEditText editIdNum;
    @Bind(R.id.tv_bank)
    TextView tvBank;
    @Bind(R.id.edit_bank_account)
    ClearEditText editBankAccount;
    @Bind(R.id.edit_mobile)
    ClearEditText editMobile;


    private String name, idNo, bankAccount, mobile;
    private long memberId;
    private BankAccount bankAcc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activiyt_add_bank_account);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        memberId= BaseApp.getInstance().getMember().getSellerId();
        titleView.setText("添加银行卡");
        bankAcc= (BankAccount) getIntent().getSerializableExtra("bank");
        if (bankAcc!=null){
            editName.setText(bankAcc.getName());
            editBankAccount.setText("");
            tvBank.setText(bankAcc.getBankName());
            editIdNum.setText(bankAcc.getIdCard());
            editMobile.setText(bankAcc.getMobile());
            bankId=bankAcc.getId();
        }
    }

    private List<Bank> bankList = new ArrayList<>();
    private long bankId;

    @OnClick({R.id.tv_bank, R.id.btn_confirm})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_bank:
                if (bankList.isEmpty()) {
                    getBankList();
                } else {
                    showBankDiog();
                }
                break;
            case R.id.btn_confirm:
                name = editName.getText().toString();
                idNo = editIdNum.getText().toString();
                bankAccount = editBankAccount.getText().toString();
                mobile = editMobile.getText().toString();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(idNo) || TextUtils.isEmpty(bankAccount) || TextUtils.isEmpty(mobile)) {
                    ToastUtils.showToast(mContext, "内容不能为空");
                    return;
                }
                if (!StringUtils.isMobileNO(mobile)){
                    ToastUtils.showToast(mContext, "请填写正确手机号");
                    return;
                }
                addBank();
                break;
        }
    }

    private void addBank() {
        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().saveBankAccount(bankId,memberId,bankAccount,name,idNo,mobile,2);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (isFinish||response.body()==null){
                    return;
                }
                if (response.body().getCode()==0){
                    ToastUtils.showToast(mContext,"添加成功");
                    finish();
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
            }
        });
    }


    private void showBankDiog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        CommenAdapter<Bank> adapter = new CommenAdapter<Bank>(mContext, bankList, R.layout.list_bank_item) {
            @Override
            public void convert(ViewHolder helper, Bank item, int position) {
                TextView bankName = helper.getView(R.id.bank_name);
                bankName.setText(item.getBankName());
            }
        };
        builder.setTitle("选择银行卡");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                tvBank.setText(bankList.get(which).getBankName());
                bankId = bankList.get(which).getId();
            }
        });
        builder.show();
    }

    private void getBankList() {
        Call<ResultDO<List<Bank>>> call = ApiClient.getApiAdapter().getBankList();
        call.enqueue(new Callback<ResultDO<List<Bank>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Bank>>> call, Response<ResultDO<List<Bank>>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    bankList.clear();
                    bankList.addAll(response.body().getResult());
                    showBankDiog();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Bank>>> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }
}
